﻿package mortal.game.view.copy.defenseCopy.view
{
    import Message.Public.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.events.*;
    import fl.controls.listClasses.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.display.*;
    import mortal.component.gconst.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.pack.*;
    import mortal.mvc.core.*;

    public class DefenseBagItemCellRenderer extends GCellRenderer
    {
        private var _item:BaseItem;

        public function DefenseBagItemCellRenderer()
        {
            var _loc_1:* = GlobalClass.getBitmap(BitmapDataConst.AlphaBMD);
            var _loc_2:* = ResourceConst.getScaleBitmap(ImagesConst.selectedBg);
            this.setStyle("downSkin", _loc_2);
            this.setStyle("overSkin", _loc_2);
            this.setStyle("upSkin", _loc_1);
            this.setStyle("selectedDownSkin", _loc_2);
            this.setStyle("selectedOverSkin", _loc_2);
            this.setStyle("selectedUpSkin", _loc_2);
            this.doubleClickEnabled = true;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._item = UIFactory.getUICompoment(BaseItem, 2, 2, this);
            this._item.setItemStyle(ItemStyleConst.Small, ImagesConst.PackItemBg, 2, 2);
            this._item.configEventListener(MouseEvent.CLICK, this.onClickHanlder);
            this._item.configEventListener(MouseEvent.DOUBLE_CLICK, this.onDoubleClickHanlder);
            this._item.doubleClickEnabled = true;
            this.configEventListener(DragEvent.Event_Move_In, this.itemMoveInHandler);
            return;
        }// end function

        private function onClickHanlder(event:MouseEvent) : void
        {
            if (this._item.itemData == null)
            {
                return;
            }
            if (CursorManager.currentCurSorType == CursorManager.DEFENSE_COPY_SELL)
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyShopSell, this._item.itemData.uid));
            }
            return;
        }// end function

        private function onDoubleClickHanlder(event:MouseEvent) : void
        {
            if (this._item.itemData == null)
            {
                return;
            }
            if (CursorManager.currentCurSorType == CursorManager.SELL)
            {
                return;
            }
            var _loc_2:Object = {};
            if (ItemsUtil.isDefenseEquip(this._item.itemData))
            {
                _loc_2.putOnUid = this._item.itemData.uid;
                _loc_2.putOffUid = "";
                _loc_2.itemData = this._item.itemData;
                _loc_2.p = this._item.localToGlobal(new Point(this._item.x, this._item.y));
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyTakeEquip, _loc_2));
                if (this._item.itemData.itemCode == GameConst.GuideDefenceEquipEquipmentCode)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PackClickedToUseItem, this._item.itemData));
                }
            }
            else if (ItemsUtil.isDefenseItemCanUse(this._item.itemData))
            {
                _loc_2.uid = this._item.itemData.uid;
                _loc_2.amount = 1;
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyUseItem, _loc_2));
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._item.itemData = null;
            this._item.dispose(param1);
            this._item = null;
            return;
        }// end function

        private function itemMoveInHandler(event:DragEvent) : void
        {
            var _loc_4:Object = null;
            var _loc_5:BaseItem = null;
            var _loc_6:ItemData = null;
            var _loc_7:DragDropData = null;
            var _loc_2:* = event.dragItem as BaseItem;
            var _loc_3:* = event.dragSouce as ItemData;
            if (_loc_3.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyRole)
            {
                _loc_4 = {};
                _loc_4.putOnUid = "";
                _loc_4.putOffUid = _loc_3.uid;
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyTakeEquip, _loc_4));
                if (_loc_3.itemCode == GameConst.GuideDefenceEquipEquipmentCode)
                {
                    Dispatcher.dispatchEvent(new DataEvent(EventName.PackClickedToUseItem, _loc_3));
                }
            }
            else if (_loc_3 && (_loc_3.posType == EPlayerItemPosType._EPlayerItemPosTypeDefenseCopyBag || _loc_3.posType == _loc_3.posType))
            {
                _loc_5 = event.dropItem as BaseItem;
                if (_loc_5 == null)
                {
                    return;
                }
                _loc_6 = _loc_5.itemData;
                _loc_7 = new DragDropData(_loc_3.serverData.posType, _loc_3.posType, _loc_3.serverData.uid, _loc_2.pos, _loc_5.pos, _loc_3, _loc_6);
                Dispatcher.dispatchEvent(new DataEvent(EventName.DefenseCopyBagMove, _loc_7));
            }
            return;
        }// end function

        override public function get data() : Object
        {
            return super.data;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            if (param1 == null)
            {
                this._item.itemData = null;
                this._item.isDragAble = false;
                return;
            }
            if (param1 is ItemData)
            {
                this._item.itemData = param1 as ItemData;
                this._item.isDragAble = true;
            }
            else if (param1 is ItemInfo)
            {
                this._item.itemCode = param1.code;
                this._item.isDragAble = true;
            }
            else
            {
                this._item.itemData = null;
                this._item.isDragAble = false;
            }
            return;
        }// end function

        override public function set listData(param1:ListData) : void
        {
            super.listData = param1;
            this._item.pos = listData.index + 1;
            return;
        }// end function

    }
}
